package cn.afactory.tygz.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsTopicInfo;


public class UserProfileTopicListAdapter extends BaseAdapter {

    private List<SnsTopicInfo> mList = new ArrayList<SnsTopicInfo>();
    private Context mContext;

    public UserProfileTopicListAdapter(Context ctx, List<SnsTopicInfo> list) {
        mList = list;
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        SnsTopicInfo snsTopicInfo = mList.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_hotchannel_list,null);

            holder.channel_icon_image = (ImageView)convertView.findViewById(R.id.channel_icon);
            holder.channel_title = (TextView)convertView.findViewById(R.id.channel_title);
            holder.channel_follower_count = (TextView)convertView.findViewById(R.id.channel_follower_count);
            holder.channel_description = (TextView)convertView.findViewById(R.id.channel_des);
            holder.add_follow = (TextView)convertView.findViewById(R.id.channel_add_follow);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        if (snsTopicInfo.topicIcon != null && snsTopicInfo.topicIcon.length() > 0) {
            BaseImgView.loadimg(holder.channel_icon_image, snsTopicInfo.topicIcon, R.drawable.ic_user_default,
                    R.drawable.ic_pic_loading_default_small, R.drawable.ic_pic_loading_default_small, ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            holder.channel_icon_image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_pic_loading_default_small));
        }

        if(snsTopicInfo.topicName != null){
            holder.channel_title.setText(snsTopicInfo.topicName);
        }

        if(snsTopicInfo.desc != null){
            holder.channel_description.setText(snsTopicInfo.desc);
        }

        if(snsTopicInfo.focus >= 0){
            holder.channel_follower_count.setText(snsTopicInfo.focus + "人关注");
        }

        return convertView;
    }


    public void setData(List<SnsTopicInfo> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    static class ViewHolder{
        public ImageView channel_icon_image;
        public TextView channel_title;
        public TextView channel_follower_count;
        public TextView channel_description;
        public TextView add_follow;
    }
}